<?php
/**
 * Created by PhpStorm.
 * 店铺自定义菜单
 * User: caicm
 * Date: 2017/6/16
 * Time: 16:39
 */

namespace WMApi\Model;


use Think\Model;

class WmStoreCustomizeMenuModel extends Model {

    /**
     * 获取店铺自定义菜单列表
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param int $is_show
     * @return mixed
     */
    public function get_menu_list($seller_id=0, $store_id=0, $is_show=0){

        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id,
            'is_show' => $is_show,
            'is_del' => 0
        );
        if($is_show==0) unset($where['is_show']);

        $field = 'menu_id,menu_name,is_show';

        return $this->field($field)->where($where)->order('sort desc,menu_id desc')->select();
    }


    /**
     * 获取店铺自定义菜单名称是否存在
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param string $menu_name
     * @param int $menu_id
     * @return mixed
     */
    public function check_menu($seller_id=0, $store_id=0, $menu_name='', $menu_id=0){

        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id,
            'menu_name' => $menu_name,
            'menu_id' => array('neq',$menu_id),
            'is_del' => 0
        );
        if($menu_id==0) unset($where['menu_id']);

        return $this->where($where)->count('menu_id');

    }

    /**
     * 获取店铺自定义菜单详情
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param int $menu_id
     * @return mixed
     */
    public function find_menu_info($seller_id=0, $store_id=0, $menu_id=0){

        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id,
            'menu_id' => $menu_id,
            'is_del' => 0
        );

        $field = 'menu_id,menu_name,is_show,sort';

        return $this->field($field)->where($where)->find();
    }

    /**
     * 店铺自定义菜单编辑
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param int $menu_id
     * @param int $sort
     * @param int $is_show
     * @param string $menu_name
     * @return bool
     */
    public function edit_menu($seller_id=0, $store_id=0, $menu_id=0, $sort=0, $is_show=0, $menu_name=''){

        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id,
            'menu_id' => $menu_id
        );

        $data = array(
            'sort' => $sort,
            'is_show' => $is_show,
            'menu_name' => $menu_name,
        );

        return $this->where($where)->data($data)->save();

    }

    /**
     * 店铺自定义菜单添加
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param int $sort
     * @param int $is_show
     * @param string $menu_name
     * @return bool
     */
    public function add_menu($seller_id=0, $store_id=0, $sort=0, $is_show=0, $menu_name=''){

        $data = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id,
            'sort' => $sort,
            'is_show' => $is_show,
            'menu_name' => $menu_name,
            'add_time' => gmtime(),
            'is_del' => 0
        );

        return $this->data($data)->add();

    }

    /**
     * 店铺自定义菜单删除
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param int $menu_id
     * @return mixed
     */
    public function del_menu($seller_id=0, $store_id=0, $menu_id=0){

        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id,
            'menu_id' => $menu_id
        );

        $data = array(
            'is_del' => 1,
        );

        return $this->where($where)->data($data)->save();
    }

    /**
     * 修改店铺自定义菜单显示状态
     * Created By caicm
     * @param int $seller_id
     * @param int $store_id
     * @param int $menu_id
     * @param int $is_show
     * @return mixed
     */
    public function update_show($seller_id=0, $store_id=0, $menu_id=0, $is_show=0){

        $where = array(
            'store_id' => $store_id,
            'seller_id' => $seller_id,
            'menu_id' => $menu_id
        );

        $data = array(
            'is_show' => $is_show,
        );

        return $this->where($where)->data($data)->save();
    }
}